package com.atguigu.gulimall.order.config;

import com.atguigu.common.constant.GulimallConstant;
import com.atguigu.common.constant.OrderConstant;
import com.atguigu.common.constant.SeckillConstant;
import com.atguigu.common.constant.WareConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuq
 * @version 1.0
 * @description:容器中的组件会自动在MQ中自动创建(RabbitMQ中没有的情况下)MQ中如果有就不会再重新创建
 * @date 2021/2/18 15:01
 */
@Configuration
public class OrderMQConfig {
    /**
     *    死信队列
     *
     *    Queue(String name,  队列名字
     *    boolean durable,  是否持久化
     *    boolean exclusive,  是否排他
     *    boolean autoDelete, 是否自动删除
     *    Map<String, Object> arguments) 属性
     *
     * String name, boolean durable, boolean exclusive, boolean autoDelete,@Nullable Map<String, Object> arguments
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put(GulimallConstant.X_DEAD_LETTER_EXCHANGE, OrderConstant.ORDER_EVENT_EXCHANGE); // 死信路由
        arguments.put(GulimallConstant.X_DEAD_LETTER_ROUTING_KEY, OrderConstant.ORDER_RELEASE_ORDER_ROUTING_KEY_NAME); // 死信路由键
        arguments.put(GulimallConstant.X_MESSAGE_TTL, OrderConstant.X_MESSAGE_TTL_TIME); // 消息过期时间 1分钟
        return new Queue(OrderConstant.ORDER_DELAY_QUEUE_NAME, true, false, false, arguments);
    }

    /**
     *  普通队列
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue(){
        return new Queue(OrderConstant.ORDER_RELEASE_ORDER_QUEUE_NAME, true, false, false);
    }


    /**
     * Topic类型的交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange(OrderConstant.ORDER_EVENT_EXCHANGE,true,false);
    }

    /**
     * 绑定死信队列
     * @return
     */
    @Bean
    public Binding orderCreateOrderBinding(){
        /*
         * String destination, 目的地（队列名或者交换机名字）
         * DestinationType destinationType, 目的地类型（Queue、Exhcange）
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         * */
        return new Binding(OrderConstant.ORDER_DELAY_QUEUE_NAME,
                Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EVENT_EXCHANGE,
                OrderConstant.ORDER_CREATE_ORDER_ROUTING_KEY_NAME,
                null);
    }

    /**
     * 绑定普通队列
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBinding(){
        return new Binding(OrderConstant.ORDER_RELEASE_ORDER_QUEUE_NAME,
                Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EVENT_EXCHANGE,
                OrderConstant.ORDER_RELEASE_ORDER_ROUTING_KEY_NAME,
                null);
    }

    /**
     *
     * 订单释放之后  直接释放库存
     * 订单的交换机绑定解锁库存的消息队列
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding(WareConstant.STOCK_RELEASE_QUEUE_NAME,
                Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EVENT_EXCHANGE,
                OrderConstant.ORDER_RELEASE_OTHER_ROUTING_KEY_NAME,
                null);
    }


    /**
     *  普通队列
     *  秒杀信息的队列
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue(){
        return new Queue(SeckillConstant.ORDER_SECKILL_ORDER_QUEUE_NAME, true, false, false);
    }

    /**
     *
     *  订单秒杀队列的绑定关系
     * @return
     */
    @Bean
    public Binding orderSeckillOrderBinding(){
        return new Binding(WareConstant.STOCK_RELEASE_QUEUE_NAME,
                Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EVENT_EXCHANGE,
                SeckillConstant.ORDER_SECKILL_ROUTING_KEY_NAME,
                null);
    }
}
